package com.xqboss.apps.service.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.constant.RedisKey;
import com.xqboss.apps.domain.order.PayAccount;
import com.xqboss.apps.enums.order.PayAccountTypeEnum;
import com.xqboss.apps.mapper.order.PayAccountMapper;
import com.xqboss.common.core.redis.RedisCache;
import com.xqboss.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PayAccountService extends ServiceImpl<PayAccountMapper, PayAccount> {

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取当前支付方式, 可能为空
     *
     * @return
     */
    public PayAccount getCurrentPayAccount(PayAccountTypeEnum accountType){
        PayAccount payAccount = redisCache.getCacheObject(RedisKey.KEY_CACHE_PAY_ACCOUNT_BY_TYPE + accountType.getValue());
        if(payAccount == null){
            payAccount = getOne(new LambdaQueryWrapper<PayAccount>()
                    .eq(PayAccount::getAccountType, accountType)
                    .eq(PayAccount::getEnable, true)
                    .orderByAsc(PayAccount::getSort)
                    .last("LIMIT 1"));
            if(payAccount != null){
                redisCache.setCacheObject(RedisKey.KEY_CACHE_PAY_ACCOUNT_BY_TYPE + accountType.getValue(), payAccount);
            }
        }
        return payAccount;
    }

    /**
     * 是否是我们自己的商户ID
     * @param appId
     * @return
     */
//    public boolean isSelfAppId(PayAccountTypeEnum accountType, String appId){
//        List<String> cacheList = redisCache.getCacheList(RedisKey.KEY_CACHE_PAY_ACCOUNT_APPID_LIST_BY_TYPE + accountType.getValue());
//        if(cacheList == null){
//            // 如果缓存列表不包含， 重新查询下数据库
//            List<PayAccount> list = list(new LambdaQueryWrapper<PayAccount>()
//                    .select(PayAccount::getAppId)
//                    .eq(PayAccount::getAccountType, accountType));
//            cacheList = list.stream().map(PayAccount::getAppId).collect(Collectors.toList());
//            redisCache.removeList(RedisKey.KEY_CACHE_PAY_ACCOUNT_APPID_LIST_BY_TYPE + accountType.getValue());
//            redisCache.setCacheList(RedisKey.KEY_CACHE_PAY_ACCOUNT_APPID_LIST_BY_TYPE + accountType.getValue(), cacheList);
//        }
//        return cacheList.contains(appId);
//    }

    /**
     * 获取支付账号信息
     *
     * @param payAccountId
     * @return
     */
    public PayAccount getPayAccount(Long payAccountId) {
        PayAccount account = redisCache.getCacheObject(RedisKey.KEY_CACHE_PAY_ACCOUNT_BY_ID + payAccountId);
        if(account == null){
            account = getById(payAccountId);
            if(account == null){
                throw new ServiceException("支付账号不存在！");
            }
            redisCache.setCacheObject(RedisKey.KEY_CACHE_PAY_ACCOUNT_BY_ID + payAccountId, account);
        }
        return account;
    }
}
